﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VisionApp.Interface;
using VisionAppManager.Model;
using System.Timers;
using CommonServiceLocator;
using Autofac;

namespace VisionAppManager.ViewModel
{
    public class VMRibbonMenu : ViewModelBase
    {
        public RelayCommand RunCommand { get; private set; }
        public RelayCommand CfgCommand { get; private set; }
        public RelayCommand SearchCommand { get; private set; }
        public RelayCommand StatisticCommand { get; private set; }
        public RelayCommand LogCommand { get; private set; }
        public RelayCommand HelpCommand { get; private set; }
        private Worker _worker;
        public Worker Worker
        {
            get
            {
                return _worker;
            }
            set
            {
                _worker = value;
                RaisePropertyChanged("Worker");
            }
        }

        private string _cfgViewMode = "Hidden";
        public string CfgViewMode
        {
            get
            {
                return _cfgViewMode;
            }
            set
            {
                _cfgViewMode = value;
                RaisePropertyChanged("CfgViewMode");
            }
        }
        private Timer _timer = null;
        public VMRibbonMenu(IWorker worker)
        {
            Worker = (Worker)worker;
            RunCommand = new RelayCommand(() => AppEngine.Container.Resolve<MainViewModel>().VMWorkspace = AppEngine.Container.Resolve<VMImgViewer>());
            CfgCommand = new RelayCommand(() => switch2CfgView());
            SearchCommand = new RelayCommand(() => AppEngine.Container.Resolve<MainViewModel>().VMWorkspace = AppEngine.Container.Resolve<VMSearch>());
            StatisticCommand = new RelayCommand(() => AppEngine.Container.Resolve<MainViewModel>().VMWorkspace = AppEngine.Container.Resolve<VMStatistic>());
            LogCommand = new RelayCommand(() => AppEngine.Container.Resolve<MainViewModel>().VMWorkspace = AppEngine.Container.Resolve<VMLog>());
            HelpCommand = new RelayCommand(() => AppEngine.Container.Resolve<MainViewModel>().VMWorkspace = AppEngine.Container.Resolve<VMHelp>());
            _timer = new Timer();
            _timer.Elapsed += _timer_Elapsed;
            _timer.Interval = 10000;
            _timer.Enabled = false;
            _timer.AutoReset = true;
            _timer.Start();
        }
        public override void Cleanup()
        {
            Worker.Exit();
            base.Cleanup();
        }
        private async void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            int ret = Worker.Init();
            if(ret==0)
            {
                _timer.Stop();
                _timer.Close();
                _timer.Dispose();
            }
        }
        private void switch2CfgView()
        {
            PermissionWindow permissionWindow = new PermissionWindow();
            permissionWindow.ShowDialog();
            if ("yfpo" == permissionWindow.Perpassword && permissionWindow.Perpassword != null)
            {
                AppEngine.Container.Resolve<MainViewModel>().VMWorkspace = AppEngine.Container.Resolve<VMCfg>();
                Worker._log.Imformation("密码输入正确,配置窗口打开。");
            }
            else if (permissionWindow.Perpassword != null)
            {
                AppEngine.Container.Resolve<MainViewModel>().VMWorkspace = AppEngine.Container.Resolve<VMCfg>();
                Worker._log.Imformation("密码输入错误！");
            }
            
        }
    }
}
